package ift605.tp3.utils.potentialyuseless;

import ift605.tp3.utils.equations.ConstantEq;
import ift605.tp3.utils.equations.IEquation;
import ift605.tp3.utils.equations.MultiplicativeEq;
import ift605.tp3.utils.equations.PowEq;
import ift605.tp3.utils.equations.SummativeEq;
import ift605.tp3.utils.equations.VariableEq;

public class DeriveUtility
{
    public static IEquation derive(IEquation eq)
    {
        if (eq instanceof ConstantEq)
        {
            return new ConstantEq(0);
        }
        if (eq instanceof VariableEq)
        {
            final VariableEq equation = (VariableEq) eq;
            return new ConstantEq(equation.getValue());
        }
        else if (eq instanceof PowEq)
        {
            final PowEq equation = (PowEq) eq;
            return new PowEq(equation.getExponent() * equation.getCoefficient(), equation.getExponent() - 1);
        }
        else if (eq instanceof SummativeEq)
        {
            final SummativeEq polyEquation = (SummativeEq) eq;
            return new SummativeEq(DeriveUtility.derive(polyEquation.getFirst()), DeriveUtility.derive(polyEquation.getSecond()));
        }
        else if (eq instanceof MultiplicativeEq)
        {
            final MultiplicativeEq multiEquation = (MultiplicativeEq) eq;
            return new SummativeEq(new MultiplicativeEq(multiEquation.getFirst(), DeriveUtility.derive(multiEquation.getSecond())), new MultiplicativeEq(DeriveUtility.derive(multiEquation.getFirst()), multiEquation.getSecond()));
        }
        else
        {
            return null;
        }
    }
}
